package com.escom.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.XYPlot;
import com.escom.R;
import com.escom.service.PlotAirflowDynamic;

import java.text.DecimalFormat;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Carlos on 21/04/2014.
 */
public class AirFlowFragment extends Fragment {

    private XYPlot dynamicPlot;
    private AirFlowObservador plotUpdater;
    //private static Thread tareaPlot;
    private PlotAirflowDynamic threadPlot;

    private class AirFlowObservador implements Observer {

        @Override
        public void update(Observable o, Object arg) {
            dynamicPlot.redraw();
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View viewActivity = inflater.inflate(R.layout.fragment_airflow, container, false);
        dynamicPlot = (XYPlot) viewActivity.findViewById(R.id.airflowDynamicPlot);
        dynamicPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("0"));
        LineAndPointFormatter graphOne = new LineAndPointFormatter(Color.rgb(0, 0, 200), null, Color.rgb(0, 0, 80), null);
        graphOne.getFillPaint().setAlpha(220);
        dynamicPlot.setGridPadding(5, 0, 5, 0);

        dynamicPlot.setRangeBoundaries(0,150, BoundaryMode.FIXED);
        dynamicPlot.setDomainBoundaries(0,30, BoundaryMode.FIXED);
        threadPlot = new PlotAirflowDynamic();

        dynamicPlot.addSeries(threadPlot, graphOne);
        plotUpdater = new AirFlowObservador();

        threadPlot.addObserver(plotUpdater);


        Thread tareaPlot = new Thread(threadPlot);
        tareaPlot.start();


        return viewActivity;
    }

    /*public void onDestroyView() {
        super.onDestroyView();
        if (tareaPlot != null && tareaPlot.isAlive()) {
            try {
                tareaPlot.sleep(1000);
                threadPlot.stopThread();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.d(Constantes.LOG_ID, this.getClass().getName() + " Hilo vivo");

        }
    }*/


}
